1
|
|
|
var InlineUpload = { |
2
|
|
|
dialog: null, |
3
|
|
|
options: { |
4
|
|
|
form_class: 'inline_upload_form', |
5
|
|
|
action: '/posts/upload', |
6
|
|
|
iframe: 'inline_upload_iframe' |
7
|
|
|
}, |
8
|
|
|
display: function(hash) { |
|
|
|
|
9
|
|
|
var self = this; |
10
|
|
|
|
11
|
|
|
this.dialog = $(document).find(".inline_upload_container"); |
12
|
|
|
|
13
|
|
|
if (!this.dialog.size()) { |
14
|
|
|
// Create invisible form and iframe |
15
|
|
|
this.dialog = $([ |
16
|
|
|
'<div style="opacity:0;position:absolute;" class="inline_upload_container"><form class="',this.options.form_class,'" action="',this.options.action,'" target="',this.options.iframe,'" method="post" enctype="multipart/form-data">', |
17
|
|
|
'<input name="upload_file" type="file" /></form>' + |
18
|
|
|
'<iframe id="',this.options.iframe,'" name="',this.options.iframe,'" class="',this.options.iframe,'" src="about:blank" width="0" height="0"></iframe></div>', |
19
|
|
|
].join('')); |
20
|
|
|
this.dialog.appendTo(document.body); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// make 'click' action on file element right after 'Picture' selection on markItUp menu |
24
|
|
|
// to show system dialog |
25
|
|
|
$("input[name='upload_file']").focus(); |
26
|
|
|
$("input[name='upload_file']").trigger('click'); |
27
|
|
|
|
28
|
|
|
// submit hidden form after file was selected in system dialog |
29
|
|
|
$("input[name='upload_file']").on('change', function(){ |
30
|
|
|
if ($(this).val() != '') { |
31
|
|
|
$('.' + self.options.form_class).submit(); |
32
|
|
|
} |
33
|
|
|
}); |
34
|
|
|
|
35
|
|
|
// response will be sent to the hidden iframe |
36
|
|
|
$('.' + this.options.iframe).bind('load', function() { |
37
|
|
|
var responseJSONStr = $(this).contents().text(); |
38
|
|
|
if (responseJSONStr != '') { |
39
|
|
|
var response = $.parseJSON(responseJSONStr); |
40
|
|
|
if (response.status == 'success') { |
41
|
|
|
var block = ['<img src="' + response.src + '" width="' + response.width + '" height="' + response.height + '" alt="" class=""/>']; |
42
|
|
|
$.markItUp({replaceWith: block.join('')} ); |
43
|
|
|
} else { |
44
|
|
|
alert(response.msg); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
self.cleanUp(); |
47
|
|
|
} |
48
|
|
|
}); |
49
|
|
|
}, |
50
|
|
|
cleanUp: function() { |
51
|
|
|
$("input[name='upload_file']").off('change'); |
52
|
|
|
this.dialog.remove(); |
53
|
|
|
} |
54
|
|
|
}; |
55
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.